/* $Header: p_ipc.c,v 2.2 01/10/02 10:27:51 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:                                                        */
/* ============                                                        */
/*                                                                     */
/* Author:                                                             */
/*                                                                     */
/* MODULE PURPOSE:                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <i_op.h>

#include <stddef.h>
#include <list_mgmt.h>

#include <std_ops.h>
#include <task_pub.h>
#include <tasks.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

       /* double linked list buffers for commumication between  */
       /* PerformSerialTransfers and background tasks           */


    QUEUE_HEAD Pneu_Free_Pool;
    QUEUE_HEAD Pneu_Resp_Queue;
    QUEUE_HEAD Pneu_Cmd_Queue;
    QUEUE_HEAD Keyboard_Cmd_Proc_Queue;
    QUEUE_HEAD DataComm_Cmd_Proc_Queue;
    QUEUE_HEAD RS_232_Cmd_Proc_Queue;
    QUEUE_HEAD Stat_Mon_Queue;
    QUEUE_HEAD Disp_Mon_Queue;

    QUEUE_HEAD Gas_Task_Queue;
    QUEUE_HEAD Seq_Task_Queue;
    QUEUE_HEAD Clk_Task_Queue;


    QUEUE_ENTRY Pneu_Buffer_Pool[NUM_FG_BG_BUFFERS];


    /* queue used to send cmds from background tasks -> Gas_Task */

    QUEUE_HEAD Gas_Task_Free_Buffers;
    QUEUE_HEAD Gas_Task_Cmds;

    GAS_TASK_MSG Gas_Task_Buffers[ NUM_GAS_TASK_BUFFERS ];



/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/



/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void QueueInit(void)

/**end_proto**/
{

    U_INT16 buffer_index;


      Pneu_Resp_Queue.Queue.Next_Entry  = (pQUEUE)&Pneu_Resp_Queue;
      Pneu_Resp_Queue.Queue.Prev_Entry  = (pQUEUE)&Pneu_Resp_Queue;
      Pneu_Resp_Queue.Num_Entries = 0;

      Pneu_Cmd_Queue.Queue.Next_Entry   = (pQUEUE)&Pneu_Cmd_Queue;
      Pneu_Cmd_Queue.Queue.Prev_Entry   = (pQUEUE)&Pneu_Cmd_Queue;
      Pneu_Cmd_Queue.Num_Entries  = 0;

      Stat_Mon_Queue.Queue.Next_Entry   = (pQUEUE)&Stat_Mon_Queue;
      Stat_Mon_Queue.Queue.Prev_Entry   = (pQUEUE)&Stat_Mon_Queue;
      Stat_Mon_Queue.Num_Entries  = 0;

      Disp_Mon_Queue.Queue.Next_Entry   = (pQUEUE)&Disp_Mon_Queue;
      Disp_Mon_Queue.Queue.Prev_Entry   = (pQUEUE)&Disp_Mon_Queue;
      Disp_Mon_Queue.Num_Entries  = 0;

      Keyboard_Cmd_Proc_Queue.Queue.Next_Entry   = (pQUEUE)&Keyboard_Cmd_Proc_Queue;
      Keyboard_Cmd_Proc_Queue.Queue.Prev_Entry   = (pQUEUE)&Keyboard_Cmd_Proc_Queue;
      Keyboard_Cmd_Proc_Queue.Num_Entries  = 0;

      DataComm_Cmd_Proc_Queue.Queue.Next_Entry   = (pQUEUE)&DataComm_Cmd_Proc_Queue;
      DataComm_Cmd_Proc_Queue.Queue.Prev_Entry   = (pQUEUE)&DataComm_Cmd_Proc_Queue;
      DataComm_Cmd_Proc_Queue.Num_Entries  = 0;

      RS_232_Cmd_Proc_Queue.Queue.Next_Entry   = (pQUEUE)&RS_232_Cmd_Proc_Queue;
      RS_232_Cmd_Proc_Queue.Queue.Prev_Entry   = (pQUEUE)&RS_232_Cmd_Proc_Queue;
      RS_232_Cmd_Proc_Queue.Num_Entries  = 0;

      Gas_Task_Queue.Queue.Next_Entry   = (pQUEUE)&Gas_Task_Queue;
      Gas_Task_Queue.Queue.Prev_Entry   = (pQUEUE)&Gas_Task_Queue;
      Gas_Task_Queue.Num_Entries  = 0;

      Seq_Task_Queue.Queue.Next_Entry   = (pQUEUE)&Seq_Task_Queue;
      Seq_Task_Queue.Queue.Prev_Entry   = (pQUEUE)&Seq_Task_Queue;
      Seq_Task_Queue.Num_Entries  = 0;

      Clk_Task_Queue.Queue.Next_Entry   = (pQUEUE)&Clk_Task_Queue;
      Clk_Task_Queue.Queue.Prev_Entry   = (pQUEUE)&Clk_Task_Queue;
      Clk_Task_Queue.Num_Entries  = 0;


      Pneu_Free_Pool.Queue.Next_Entry   = (pQUEUE)&Pneu_Free_Pool;
      Pneu_Free_Pool.Queue.Prev_Entry   = (pQUEUE)&Pneu_Free_Pool;
      Pneu_Free_Pool.Num_Entries  = 0;

      for ( buffer_index = 0; buffer_index < NUM_FG_BG_BUFFERS ; buffer_index++ )
      {
           LinkBufferToTail( &Pneu_Buffer_Pool[buffer_index], &Pneu_Free_Pool );
      }



      Gas_Task_Cmds.Queue.Next_Entry   = (pQUEUE)&Gas_Task_Cmds;
      Gas_Task_Cmds.Queue.Prev_Entry   = (pQUEUE)&Gas_Task_Cmds;
      Gas_Task_Cmds.Num_Entries  = 0;

      Gas_Task_Free_Buffers.Queue.Next_Entry   = (pQUEUE)&Gas_Task_Free_Buffers;
      Gas_Task_Free_Buffers.Queue.Prev_Entry   = (pQUEUE)&Gas_Task_Free_Buffers;
      Gas_Task_Free_Buffers.Num_Entries  = 0;

      for ( buffer_index = 0; buffer_index < NUM_GAS_TASK_BUFFERS ; buffer_index++ )
      {
           LinkBufferToTail( &Gas_Task_Buffers[buffer_index], &Gas_Task_Free_Buffers );
      }



}

/***********************************************************************/
/* FUNCTION: GetResponseQueuePtr                                       */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: returns a pointer to the pneu response queue for this      */
/*          process.                                                   */
/*                                                                     */
/*                                                                     */
/* REVISIT should return NULL if called from foreground                */
/*                                                                     */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

pQUEUE_HEAD GetResponseQueuePtr(void)

/**end_proto**/
{

     switch ( TASK_Get_MyName() )
     {
        case STAT_MON_NAME:  return &Stat_Mon_Queue;
        case DISP_MON_NAME:  return &Disp_Mon_Queue;
        case KBD_CMD_NAME:   return &Keyboard_Cmd_Proc_Queue;
        case MIO_CMD_NAME:   return &DataComm_Cmd_Proc_Queue;
        case RS232_CMD_NAME: return &RS_232_Cmd_Proc_Queue;
        case GAS_PASS_NAME:  return &Gas_Task_Queue;
        case SEQUENCE_NAME:  return &Seq_Task_Queue;
        case CLOCK_TBL_NAME: return &Clk_Task_Queue;
        default:             return NULL;  /* a foreground task */
     }
}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void WaitForPneuResponse(void)

/**end_proto**/
{

    U_INT16 buffer_count;

#define WaitOnQueue(QUEUE)                    \
   buffer_count = 0;                          \
   while ( buffer_count == 0 )                \
   {                                          \
        TASK_Yield();                         \
        CountBuffers(QUEUE,buffer_count);     \
   }                                          \




     switch ( TASK_Get_MyName() )
     {
        case STAT_MON_NAME:  WaitOnQueue( &Stat_Mon_Queue );
                             break;
        case DISP_MON_NAME:  WaitOnQueue( &Disp_Mon_Queue );
                             break;
        case KBD_CMD_NAME:   WaitOnQueue( &Keyboard_Cmd_Proc_Queue );
                             break;
        case MIO_CMD_NAME:   WaitOnQueue( &DataComm_Cmd_Proc_Queue );
                             break;
        case RS232_CMD_NAME: WaitOnQueue( &RS_232_Cmd_Proc_Queue );
                             break;
        case GAS_PASS_NAME:  WaitOnQueue( &Gas_Task_Queue );
                             break;
        case SEQUENCE_NAME:  WaitOnQueue( &Seq_Task_Queue );
                             break;
        case CLOCK_TBL_NAME: WaitOnQueue( &Clk_Task_Queue );
                             break;
        default:  __asm( " trap #3" ); /* called from an unsupported task */

     }

}
